package com.mallcloud.mall.app.config;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiModelProperty;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Objects;

/**
 * 过程Mapper 处理前后数据编码格式问题
 */
@Slf4j
@Aspect
@Service
public class CharacterAppointAspect {

	@Pointcut("execution(* com.mallcloud.mall.app.mapper.AppProcedureMapper.*(..))")
	public void pointCut() { }

	/**
	 * getArgs(): 返回方法参数
	 */
	@SneakyThrows
	@Around("pointCut()")
	public Object beforePointCut(ProceedingJoinPoint joinPoint) {
		String methodName = "";

		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Method method = signature.getMethod();
		ApiModelProperty annotation = method.getAnnotation(ApiModelProperty.class);
		if(Objects.nonNull(annotation)){
			methodName = annotation.value();
		}

		// 过程请求 目前因为只有一个入参
		Object dto = joinPoint.getArgs()[0];
		if(Objects.nonNull(dto)){
			log.info("{}请求:{}", methodName, JSON.toJSONString(dto));
//			OracleCharEncodingUtils.toOracle(dto);
		}

		Object obj = joinPoint.proceed();

		if(Objects.nonNull(dto)){
			log.info("{}返回:{}", methodName, JSON.toJSONString(dto));
		}

		return obj;
	}
}
